package edu.xpu.cs.hub.controller;

import com.alibaba.fastjson.JSON;
import edu.xpu.cs.hub.entity.Node;
import edu.xpu.cs.hub.entity.Process;
import edu.xpu.cs.hub.service.NodeService;
import edu.xpu.cs.hub.service.ProcessService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.io.IOException;
import java.util.Date;
import java.util.List;
import java.util.UUID;


@Controller
public class MyfileController {


//    节点------------------------------------------------------------------------------
    @Autowired
    private NodeService nodeService;
    private String url;

    @PostMapping(value="/node/uploadNode",produces="application/json;charset=UTF-8")
    public @ResponseBody String uploadFile(@RequestParam("fileName") MultipartFile file) {
        System.out.print("上传文件==="+"\n");
        //判断文件是否为空
        if (file.isEmpty()) {
            return "上传文件不可为空";
        }

        // 获取文件名
        String fileName = file.getOriginalFilename();
        System.out.print("上传的文件名为: "+fileName+"\n");
//        fileName = new SimpleDateFormat("yyyyMMddHHmmss").format(new Date()) + "_" + fileName;
//        System.out.print("（加个时间戳，尽量避免文件名称重复）保存的文件名为: "+fileName+"\n");
        System.out.print("保存的文件名为: "+fileName+"\n");
        //加个时间戳，尽量避免文件名称重复
        String location = "D:\\file\\" +fileName;
        //文件绝对路径
        System.out.print("保存文件绝对路径"+location+"\n");
        //创建文件路径
        File dest = new File(location);

        //判断文件是否已经存在
        if (dest.exists()) {
            return "文件已经存在";
        }
        //判断文件父目录是否存在
        if (!dest.getParentFile().exists()) {
            dest.getParentFile().mkdir();
        }
        try {
            //上传文件
            file.transferTo(dest); //保存文件
            System.out.print("保存文件路径"+location+"\n");
            url="http://localhost:8080/nodes/nodesInfo"+fileName;
            UUID uuid=UUID.randomUUID();
            String id=uuid.toString();
            Date date = new Date();
            String category="IO";
            int result= nodeService.insertNode2sql(id,fileName,url,location,date,category);
            System.out.print("插入结果"+result+"\n");
            System.out.print("保存的完整url===="+url+"\n");
        } catch (IOException e) {
            return "上传失败";
        }
        String jsonNode = JSON.toJSONString("上传成功,文件url=="+url);
        return jsonNode;
    }

    //查询
    @GetMapping("/node/listAllNodes")
    public String listAll(Model model){
          System.out.print("查询结点"+"\n");
          List<Node> nodes=nodeService.selectAllNode();
          System.out.print("查询到的结点数量=="+nodes.size()+"\n");
          model.addAttribute("nodes", nodes);
          return "filelist";
    }


//    流程------------------------------------------------------------------------------
    @Autowired
    private ProcessService processService;
    private String processurl;

    @PostMapping(value="/process/upload",produces="application/json;charset=UTF-8")
    public @ResponseBody String uploadProcessFile(@RequestParam("fileName") MultipartFile file) {
        System.out.print("上传文件==="+"\n");
        //判断文件是否为空
        if (file.isEmpty()) {
            return "上传文件不可为空";
        }

        // 获取文件名
        String fileName = file.getOriginalFilename();
        System.out.print("上传的文件名为: "+fileName+"\n");
        System.out.print("保存的文件名为: "+fileName+"\n");
        //加个时间戳，尽量避免文件名称重复
        String location = "D:\\file\\" +fileName;
        //文件绝对路径
        System.out.print("保存文件绝对路径"+location+"\n");
        //创建文件路径
        File dest = new File(location);

        //判断文件是否已经存在
        if (dest.exists()) {
            return "文件已经存在";
        }
        //判断文件父目录是否存在
        if (!dest.getParentFile().exists()) {
            dest.getParentFile().mkdir();
        }
        try {
            //上传文件
            file.transferTo(dest); //保存文件
            System.out.print("保存文件路径"+location+"\n");
            url="http://localhost:8080/process/processInfo"+fileName;
            UUID uuid=UUID.randomUUID();
            String id=uuid.toString();
            Date date = new Date();
            String category="IO";
            int result= processService.insertProcess2sql(id,fileName,url,location,date,category);
            System.out.print("插入结果"+result+"\n");
            System.out.print("保存的完整url===="+url+"\n");
        } catch (IOException e) {
            return "上传失败";
        }
        String jsonWorkflow = JSON.toJSONString("上传成功,文件url=="+url);
        return jsonWorkflow;
    }

    //查询
    @GetMapping("/process/listAll")
    public String listAllProcess(Model model){
          System.out.print("查询流程"+"\n");
          List<Process> process=processService.selectAll();
          System.out.print("查询到的流程数量=="+process.size()+"\n");
          model.addAttribute("process", process);
          return "processlist";
    }

    @GetMapping("/node/getNode")
    public String getNode(){
        return "officehub";
    }

}
